package com.tuanzi.loan.web.bean.system;

import com.tuanzi.loan.business.entity.system.Dictionary;
import com.tuanzi.loan.business.service.system.DictionaryService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class DictionaryInfoBean extends JSFInfoBean<Dictionary> {

    private static final long serialVersionUID = -2152778554108785342L;
    @Autowired
    private DictionaryService dictionaryService;

    @PostConstruct
    private void init() {
        String id = getParamFromRequest("id");
        if (StringUtils.isNotBlank(id)) {
            $ = dictionaryService.findOne(id);
        } else {
            $ = new Dictionary();
            changeStatusToAdd();
        }
    }

    public void onSave() {
        $ = dictionaryService.saveOrUpdate($);
        changeStatusToView();
        addSuccessfullyMessage();
    }
    public void onCopy() {
        $.setId(null);
        $.setCreator(null);
        $.setCreateTime(null);
        $.setUpdator(null);
        $.setUpdateTime(null);
        $.setVersion(null);
        changeStatusToEdit();
    }
}
